<?php
 namespace data\service;

 use data\model\User as UserModel;
 use think\facade\Session;

 class UserService extends BaseService{
     private $userModel;

     // 模型初始化
     public function __construct()
     {
        $this->userModel = new UserModel;
     }

     /**
      * 检查用户信息是否正确
      * @desc
      * @param $post
      * @return array|bool|null|\PDOStatement|string|\think\Model
      * @author 16
      * @date 2018/2/24
      */
     public function checkUser($post){
        if($user = $this->userModel->getUserByUsername($post['username'])){
            if (md5($post['psw']) == $user->psw)
            {
                return $user;
            }else{
                $this->errMsg = '密码错误!';
            }
        }else{
            $this->errMsg = '用户不存在';
        }

        return false;
     }

     /**
      * 存储登陆信息
      * @author 16
      * @date 2018-2-24
      */
     public function setLoginStatus($user){
         Session::set('user',$user);
         Session::set('prevLoginInfo',cache('login_'.$user->username));  //获取上次登录的信息

         $loginInfo = [
             'login_time' => time(),
             'login_address' => getUserAddressByIp(),
             'ip'=>request()->ip()
         ];

         cache('login_'.$user->username,$loginInfo);    //存储本次登录的信息
     }

 }